package com.xinsoft.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinsoft.constant.Const;
import com.xinsoft.entity.dto.SysLogExportParam;
import com.xinsoft.entity.dto.SysLogParam;
import com.xinsoft.entity.po.SysOperLog;
import com.xinsoft.mapper.SysOperLogMapper;
import com.xinsoft.service.SysOperLogService;
import com.xinsoft.utils.ExcelUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author lijie
 * @description 针对表【sys_oper_log(操作日志记录)】的数据库操作Service实现
 * @createDate 2023-05-15 12:56:56
 */
@Service
public class SysOperLogServiceImpl extends ServiceImpl<SysOperLogMapper, SysOperLog>
        implements SysOperLogService {

    @Resource
    SysOperLogMapper logMapper;

    @Override
    public Object findPageListByParam(SysLogParam param) {
        //分页
        Page<SysOperLog> page = new Page<SysOperLog>(param.getCurrentPage(), param.getPageSize());
        LambdaQueryWrapper<SysOperLog> queryWrapper = new LambdaQueryWrapper<>();
        //查询条件
        queryWrapper
                .eq(param.getUserId() != null, SysOperLog::getOperUid, param.getUserId())
                .gt(StrUtil.isNotBlank(param.getStartTime()), SysOperLog::getOperTime, param.getStartTime())
                .lt(StrUtil.isNotBlank(param.getEndTime()), SysOperLog::getOperTime, param.getEndTime())
                .orderByDesc(SysOperLog::getOperTime);
        IPage<SysOperLog> resultPage = logMapper.selectPage(page, queryWrapper);
        //返回值
        return resultPage;
    }

    @Override
    public void exportLogList(SysLogExportParam param, HttpServletResponse response) throws Exception {
        LambdaQueryWrapper<SysOperLog> queryWrapper = new LambdaQueryWrapper<>();
        //查询条件
        queryWrapper
                .eq(param.getUserId() != null, SysOperLog::getOperUid, param.getUserId())
                .gt(StrUtil.isNotBlank(param.getStartTime()), SysOperLog::getOperTime, param.getStartTime())
                .lt(StrUtil.isNotBlank(param.getEndTime()), SysOperLog::getOperTime, param.getEndTime())
                .orderByDesc(SysOperLog::getOperTime);
        List<SysOperLog> list = logMapper.selectList(queryWrapper);
        for (SysOperLog log : list) {
            //状态为正常
            if(Const.N.equals(log.getStatus())){
                log.setStatusName("正常");
            }else if(Const.Y.equals(log.getStatus())){
                //状态为异常
                log.setStatusName("异常");
            }
        }
        //导出excel
        ExcelUtils.exportByEasypoi(list, SysOperLog.class, "系统日志记录", "系统日志记录", response,"系统日志记录");
    }

}




